package org.jeecg.common.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    public static String datePattern = "yyyy-MM-dd";
    public static String timePattern = "HH:mm:ss";
    public static String dateTimePattern = "yyyy-MM-dd HH:mm:ss";

    public static String getDate(Date date)
    {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (date != null) {
            df = new SimpleDateFormat(datePattern);
            returnValue = df.format(date);
        }

        return returnValue;
    }

    public static String getChineseDate(String date)
    {
        String redate = date.substring(0, 4) + "年" + date.substring(5, 7) + "月" + date.substring(8, 10) + "日";
        return redate;
    }

    public static String getDate(Date date, String mask)
    {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (date == null) {
        } else {
            df = new SimpleDateFormat(mask);
            returnValue = df.format(date);
        }

        return returnValue;
    }

    public static String getCurDate()
    {
        Calendar today = Calendar.getInstance();
        return getDate(today.getTime(), "yyyy-MM-dd");
    }

    public static String getCurChineseDate()
    {
        Calendar today = Calendar.getInstance();
        return getDate(today.getTime(), "yyyy年MM月dd日");
    }

    public static String getCurDateTime()
    {
        Calendar today = Calendar.getInstance();
        return getDate(today.getTime(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String getCurPlainDateTime()
    {
        Calendar today = Calendar.getInstance();
        return getDate(today.getTime(), "yyyyMMddHHmmss");
    }

    public static String getCurPlainDate()
    {
        Calendar today = Calendar.getInstance();
        return getDate(today.getTime(), "yyyyMMdd");
    }

    public static String getCurYearMonth()
    {
        Calendar today = Calendar.getInstance();
        return getDate(today.getTime(), "yyyyMM");
    }

    public static String getCurChineseYearMonth()
    {
        Calendar today = Calendar.getInstance();
        return getDate(today.getTime(), "yyyy年MM月");
    }

    public static String getCurFirstDay()
    {
        return getFirstDay(getCurDate());
    }

    public static String getFirstDay(String strDate)
    {
        int year = Integer.parseInt(strDate.substring(0, 4));
        int month = 0;
        if (strDate.indexOf("-") > 0) {
            if ((strDate.length() == 6) || (strDate.length() == 7))
                month = Integer.parseInt(strDate.substring(5));
            else
                month = Integer.parseInt(strDate.substring(5, strDate.lastIndexOf("-")));
        }
        else if (strDate.length() == 8)
            month = Integer.parseInt(strDate.substring(4, 6));
        else {
            month = Integer.parseInt(strDate.substring(4));
        }

        String strMonth = month+"";
        if (month < 10)
            strMonth = "0" + strMonth;
        return String.valueOf(year) + "-" + strMonth + "-01";
    }

    public static String getCurLastDay()
    {
        return getLastDay(getCurDate());
    }

    public static String getLastDay(String strDate)
    {
        int year = Integer.parseInt(strDate.substring(0, 4));
        int month = 0;
        if (strDate.indexOf("-") > 0) {
            if ((strDate.length() == 6) || (strDate.length() == 7))
                month = Integer.parseInt(strDate.substring(5));
            else
                month = Integer.parseInt(strDate.substring(5, strDate.lastIndexOf("-")));
        }
        else if (strDate.length() == 8)
            month = Integer.parseInt(strDate.substring(4, 6));
        else {
            month = Integer.parseInt(strDate.substring(4));
        }

        String lastDay = "";
        if (month == 2) {
            if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)))
                lastDay = "29";
            else
                lastDay = "28";
        } else if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
            lastDay = "30";
        else
            lastDay = "31";
        String strMonth = month + "";
        if (month < 10)
            strMonth = "0" + strMonth;
        return String.valueOf(year) + "-" + strMonth + "-" + lastDay;
    }

    public static boolean isLeapYear(String strYear)
    {
        int year = Integer.parseInt(strYear);

        return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
    }

    public static int compareDate(String strFrom, String strTo)
    {
        Date dateFrom = getDate(strFrom);
        Date dateTo = getDate(strTo);
        if (dateFrom.before(dateTo))
            return -1;
        if (dateFrom.after(dateTo)) {
            return 1;
        }
        return 0;
    }

    public static Date getDate(String strDate)
    {
        String str = strDate;
        if (strDate.indexOf('-') < 0) {
            str = strDate.substring(0, 4) + "-" + strDate.substring(4, 6) + "-" + strDate.substring(6, 8);
        }
        return java.sql.Date.valueOf(str);
    }

    public static String getStepYear(int n)
    {
        Calendar c = Calendar.getInstance();
        c.add(1, n);
        return fillStrDate(c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5));
    }

    public static String getStepMonth(int n)
    {
        Calendar c = Calendar.getInstance();
        c.add(2, n);
        return fillStrDate(c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5));
    }

    public static String getStepDay(int n)
    {
        Calendar c = Calendar.getInstance();
        c.add(5, n);
        return fillStrDate(c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5));
    }

    public static String getSetpDay(String date, int n)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(getDate(date));
        c.add(5, n);
        return fillStrDate(c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5));
    }

    public static String getStepMonth(String date, int n)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(getDate(date));
        c.add(2, n);
        return fillStrDate(c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5));
    }

    public static String getStepYear(String date, int n)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(getDate(date));
        c.add(1, n);
        return fillStrDate(c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5));
    }

    private static String fillStrDate(String simpleDate)
    {
        StringBuffer redate = new StringBuffer(simpleDate.substring(0, 4)).append("-");
        String month = simpleDate.substring(5, simpleDate.lastIndexOf("-"));
        if (month.length() == 1)
            redate.append("0");
        redate.append(month).append("-");
        String day = simpleDate.substring(simpleDate.lastIndexOf("-") + 1);
        if (day.length() == 1)
            redate.append("0");
        redate.append(day);
        return redate.toString();
    }

    public static String getDateFromTimeStamp(String timestamp, String mask)
    {
        Long unixtime = Long.valueOf(Long.parseLong(timestamp) * 1000L);
        String date = new SimpleDateFormat(mask).format(new Date(unixtime.longValue()));
        return date;
    }

    public static Date getBeforeFirstMonthdate(){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getBeforeLastMonthdate(){
        Calendar calendar=Calendar.getInstance();
        int month=calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month-1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     *
     * 获取本周第一天
     * @return
     */
    public static String getCurWeekFirstDay(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        Calendar cld = Calendar.getInstance(Locale.CHINA);
        cld.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
        cld.setTimeInMillis(System.currentTimeMillis());//当前时间
        cld.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//周一
        return df.format(cld.getTime());
    }

    /**
     * 获取本周最后一天
     * @return
     */
    public static String getCurWeekLastDay(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        Calendar cld = Calendar.getInstance(Locale.CHINA);
        cld.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
        cld.setTimeInMillis(System.currentTimeMillis());//当前时间
        cld.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//周日
        return df.format(cld.getTime());
    }

    /**
     *
     * 获取本月第一天
     * @return
     */
    public static String getCurMonthFirstDay(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//1:本月第一天
        return format.format(c.getTime());
    }

    /**
     *
     * 获取本月最后一天
     * @return
     */
    public static String getCurMonthLastDay(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(ca.getTime());
    }

}
